package org.apache.james;

import com.github.steveash.guavate.Guavate;
import org.apache.james.core.Domain;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.domainlist.api.DomainListException;
import org.apache.james.domainlist.lib.AbstractDomainList;
import org.apache.james.mapper.CustomDomainMapper;
import org.apache.james.model.CustomDomain;

import javax.inject.Inject;
import java.util.List;

/**
 * 域名列表
 * @author ccoke
 */
public class CustomDomainList extends AbstractDomainList {
    private final CustomDomainMapper customDomainMapper;

    @Inject
    public CustomDomainList(DNSService dns, CustomDomainMapper customDomainMapper) {
        super(dns);
        this.customDomainMapper = customDomainMapper;
    }

    @Override
    protected List<Domain> getDomainListInternal() throws DomainListException {
        List<CustomDomain> domains = customDomainMapper.selectList();
        return domains.stream().map(domain -> Domain.of(domain.getName())).collect(Guavate.toImmutableList());
    }

    @Override
    protected boolean containsDomainInternal(Domain domain) throws DomainListException {
        return customDomainMapper.selectCountByName(domain.name()) > 0;
    }

    @Override
    public void addDomain(Domain domain) throws DomainListException {
        // 不支持
    }

    @Override
    public void removeDomain(Domain domain) throws DomainListException {
        // 不支持
    }
}
